----------------------------------------------------------------------------------------------------------------------------------------------------
--[[
        乾坤泰卦帽：
    胡桃的专属帽子，自带四个格子，但是不可以看做背包。
    胡桃出场时自带，绑定拥有者ID，只有拥有者可以与其交互，并且会跟拥有者一块下线。同一ID只能拥有一顶，拥有者获得新的乾坤泰卦帽之后会立刻摧毁旧的。
]]--
----------------------------------------------------------------------------------------------------------------------------------------------------
---
    local assets =
    {
        Asset("ANIM", "anim/cane.zip"),
        Asset("ANIM", "anim/swap_cane.zip"),
    }
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 穿脱
    local function onequip(inst, owner)

    end

    local function onunequip(inst, owner)

    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- hook equipable
    local function equipable_IsRestricted(equipable_com,target)
        --return true if restricted (can't equip)
        if target:HasTag("hutao") then 
            local replica = equipable_com.inst.replica.hutao_com_cosmic_hat_sys_for_item or equipable_com.inst.replica._.hutao_com_cosmic_hat_sys_for_item              
            if replica and replica:GetLinkedPlayer() == target then
                return false
            else
                print("error : hat replica miss")
            end
        end
        return true
    end
    local function hook_equipable(inst,replica_com)
        replica_com.IsRestricted = equipable_IsRestricted
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- container install
    local function install_container(inst)
        local fn = require("prefabs/06_equipments/05_02_cosmic_hat_container")
        if type(fn) == "function" then
            fn(inst)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 内部物品 逻辑
    local function inner_item_logic(inst)
        local data = require("prefabs/06_equipments/05_03_cosmic_hat_item_logic")
        if type(data) == "table" and type(data.fn) == "function" then
            data.fn(inst)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 霉运系统
    local function bad_luck_sys_install(inst)
        local fn_table = require("prefabs/06_equipments/05_04_cosmic_hat_bad_luck")
        local fn = fn_table and fn_table.sky or nil
        if type(fn) == "function" then
            fn(inst)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 转换控制
    local function tran_2_another_hat(inst)
        local fn_table = require("prefabs/06_equipments/05_05_cosmic_hat_transfrom")
        local fn = fn_table and fn_table.sky or nil
        if type(fn) == "function" then
            fn(inst)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 本体
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        MakeInventoryPhysics(inst)

        inst.AnimState:SetBank("cane")
        inst.AnimState:SetBuild("swap_cane")
        inst.AnimState:PlayAnimation("idle")

        inst:AddTag("irreplaceable")
        inst:AddTag("nonpotatable")
        inst:AddTag("sky")

        local swap_data = {sym_build = "swap_cane"}
        MakeInventoryFloatable(inst, "med", 0.05, {0.85, 0.45, 0.85}, true, 1, swap_data)


        inst.entity:SetPristine()
        -----------------------------------------------------
        --- 
            install_container(inst)
            inner_item_logic(inst)
            tran_2_another_hat(inst)
        -----------------------------------------------------
        --- 
            inst:ListenForEvent("HUTAO_OnEntityReplicated.equippable",hook_equipable)
        -----------------------------------------------------
        if not TheWorld.ismastersim then
            return inst
        end
        -----------------------------------------------------
        --- 保鲜控制器
            inst:AddComponent("preserver")
        -----------------------------------------------------
        --- 防水
            inst:AddTag("waterproofer")            
            inst:AddComponent("waterproofer")
            inst.components.waterproofer:SetEffectiveness(0)
        -----------------------------------------------------
        --- 
            inst:AddComponent("hutao_com_cosmic_hat_sys_for_item")
            inst.components.hutao_com_cosmic_hat_sys_for_item:SetMax(777.7)
            bad_luck_sys_install(inst)
        -----------------------------------------------------
        --- 调用梅花的冷热源
            inst:AddComponent("hutao_com_flower_switcher")
            inst.components.hutao_com_flower_switcher:SetIsFlower(false)
        -----------------------------------------------------
        --- 
            inst:AddComponent("named")
            inst:AddComponent("inspectable")
            inst:AddComponent("inventoryitem")
            inst.components.inventoryitem:ChangeImageName("tophat")
            -- inst.components.inventoryitem.imagename = "hutao_cooked_food_excample"
            -- inst.components.inventoryitem.atlasname = "images/inventoryimages/hutao_cooked_food_excample.xml"
        -----------------------------------------------------
        --- 穿戴
            inst:AddComponent("equippable")
            inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
            inst.components.equippable:SetOnEquip(onequip)
            inst.components.equippable:SetOnUnequip(onunequip)
        -----------------------------------------------------
            MakeHauntableLaunch(inst)
        -----------------------------------------------------
        return inst
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
return Prefab("hutao_equipment_cosmic_hat_sky", fn, assets)
